package app.auto.runner.base.utility;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.telecom.Call;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import app.auto.runner.base.DialogUtil;
import app.auto.runner.base.intf.FunCallback;
import app.auto.runner.base.intf.ListBuilder;


/**
 * Created by hp on 2018/2/22.
 */

public class ShareAlert {

    public static void shareAlert(final Activity ctx, final String url, final String params, final String result, final String title) {
        ctx.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ShareAlert.shareAlert(ctx, "URL: \n" + url + "\n" + "Params: \n" + params + "\nResult: \n" + result + "\n" + title);
            }
        });

    }


    static Dialog dialog;
    public static void shareAlert(final Activity ctx, final String model, final String txt, final FunCallback func) {
        new Handler(Looper.getMainLooper(), new android.os.Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) { if (dialog != null) {
                dialog.cancel();
                dialog.dismiss();
                dialog = null;
            }
                dialog = new AlertDialog.Builder(ctx).setMessage(model).setCancelable(true)
                        .setNeutralButton("分享", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent sendIntent = new Intent();
                        sendIntent.setAction(Intent.ACTION_SEND);
                        sendIntent.putExtra(Intent.EXTRA_TEXT, model);
                        sendIntent.setType("text/plain");
                        try {

                            Intent chooserIntent = Intent.createChooser(sendIntent, "选择分享途径");
                            if (chooserIntent == null) {
                                return;
                            }
                            ctx.startActivity(chooserIntent);
                        } catch (Exception e) {
                        }
                        dialogInterface.dismiss();
                        dialogInterface.cancel();


                    }
                }).setNegativeButton(txt,new DialogUtil.DialogClicker(){
                            @Override
                            public void onClick(DialogInterface dialog, int arg1) {
                                super.onClick(dialog, arg1);
                                func.simpleRun(func.getObject());
                            }
                        }).create();
                dialog.show();
                return false;
            }
        }).sendEmptyMessage(0);
    }
    public static void shareAlert(final Activity ctx, final String model) {
        new Handler(Looper.getMainLooper(), new android.os.Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) { if (dialog != null) {
                dialog.cancel();
                dialog.dismiss();
                dialog = null;
            }
                dialog = new AlertDialog.Builder(ctx).setMessage(model).setCancelable(true).setNeutralButton("分享", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent sendIntent = new Intent();
                        sendIntent.setAction(Intent.ACTION_SEND);
                        sendIntent.putExtra(Intent.EXTRA_TEXT, model);
                        sendIntent.setType("text/plain");
                        try {

                            Intent chooserIntent = Intent.createChooser(sendIntent, "选择分享途径");
                            if (chooserIntent == null) {
                                return;
                            }
                            ctx.startActivity(chooserIntent);
                        } catch (Exception e) {
                        }
                        dialogInterface.dismiss();
                        dialogInterface.cancel();


                    }
                }).create();
                        dialog.show();
                return false;
            }
        }).sendEmptyMessage(0);
    }

    public static void sureAlert(final Activity ctx, final String model, final FunCallback surefuc) {
        new Handler(Looper.getMainLooper(), new android.os.Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) {
                new AlertDialog.Builder(ctx).setMessage(model).setCancelable(true).setPositiveButton("分享", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Intent sendIntent = new Intent();
                        sendIntent.setAction(Intent.ACTION_SEND);
                        sendIntent.putExtra(Intent.EXTRA_TEXT, model);
                        sendIntent.setType("text/plain");
                        try {

                            Intent chooserIntent = Intent.createChooser(sendIntent, "选择分享途径");
                            if (chooserIntent == null) {
                                return;
                            }
                            ctx.startActivity(chooserIntent);
                        } catch (Exception e) {
                        }


                    }
                }).setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        surefuc.simpleRun(null);
                        dialog.dismiss();
                        dialog.cancel();
                    }
                }).create().show();
                return false;
            }
        }).sendEmptyMessage(0);
    }


    static AlertDialog ad = null;

    public static void delAlert(final Activity ctx, final FunCallback fcb) {
        if (ad != null) {
            ad.dismiss();
            ad.cancel();
            ad = null;
        }


        new Handler(Looper.getMainLooper(), new android.os.Handler.Callback() {

            @Override
            public boolean handleMessage(Message msg) {

                ad =
                        new AlertDialog.Builder(ctx).setNeutralButton("删除\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                                dialogInterface.cancel();
                                ad = null;
                                fcb.simpleRun(null);
                            }
                        }).setCancelable(true).create();
                ad.show();
                return false;
            }
        }).sendEmptyMessage(0);
    }

}
